/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jastrzab.core;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.AsyncTask;
import android.os.ConditionVariable;
import android.util.Log;
import android.view.SurfaceHolder;

/**
 *
 * @author Jastrzab
 */
public final class CameraWindow extends AsyncTask<Object, Boolean, Object> implements CameraContant {

	private final ConditionVariable mFlowControl = new ConditionVariable();
	private final Context mContext;
	private SurfaceTexture mSurface;
	private SurfaceHolder mHolder;
	private Camera mCamera;
	private Parameters mParamter;
	private PreviewFormat mSize;

	public CameraWindow(Context context) {
		mContext = context;
		if (CameraImplements.isSurfaceTexture()) {
			mSurface = CameraImplements.getSurfaceTexture();
		} else {
			mHolder = CameraImplements.getSurfaceHolder(context);
		}
	}

	public void baginProcesing() {
		try {
			mFlowControl.close();
			execute();
		} catch (IllegalThreadStateException e) {
			Log.d(LOG, "Camera Alredy Running");
		}
	}

	public void endProcesing() {
		mFlowControl.open();
	}

	public Context getContext() {
		return mContext;
	}

	public Camera getCamera() {
		return mCamera;
	}

	public boolean isCamera() {
		return mCamera != null;
	}

	@Override
	protected Object doInBackground(Object... object) {
		openCamera();
		if (!isCamera()) {
			return null;
		}
		RemainWake.getLock(mContext);
		try {
			mParamter = mCamera.getParameters();
			mParamter.setFlashMode(CameraImplements.getLightSuported(mParamter));
			mSize = PreviewFormat.getMinSize(mParamter);
			mParamter.setPreviewSize(mSize.mWidth, mSize.mHeight);
			mParamter.setPreviewFormat(ImageFormat.NV21);
			mCamera.setParameters(mParamter);
			if (CameraImplements.isSurfaceTexture()) {
				mCamera.setPreviewTexture(mSurface);
			} else {
				mCamera.setPreviewDisplay(mHolder);
			}
			mCamera.startPreview();
			mFlowControl.block();
			mCamera.stopPreview();
		} catch (Exception e) {
			Log.d(LOG, "Exception In Camera Thread " + e.getLocalizedMessage());
		} finally {
			closeCamera();
			RemainWake.leaveLock();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Object result) {
		if (isCamera()) {
			Log.d(LOG, "Working Camera Found Close");
			closeCamera();
		}
	}

	private void openCamera() {
		Log.d(LOG, "Camera Created");
		try {
			mCamera = Camera.open();
		} catch (Exception e) {
			Log.d(LOG, "Can Not Connect To Camera " + e.getLocalizedMessage());
		}
	}

	private void closeCamera() {
		Log.d(LOG, "Camera Destroy");
		try {
			mCamera.release();
			mCamera = null;
		} catch (Exception e) {
			Log.d(LOG, "Can Not Close Camera " + e.getLocalizedMessage());
		}
	}

}
